<?php

if (!defined('IN_IA')) {
	exit('Access Denied');
}

class Member_EweiShopV2Page extends PluginMobileLoginPage 
{
	public function main()
	{
		global $_W;
		global $_GPC;
		$this->diypage('member');
		$member = m('member')->getMember($_W['openid'], true);
		$level = m('member')->getLevel($_W['openid']);
		$open_creditshop = p('creditshop') && $_W['shopset']['creditshop']['centeropen'];
		$params = array(':uniacid' => $_W['uniacid'], ':openid' => $_W['openid']);
		$merch_plugin = p('merch');
		$merch_data = m('common')->getPluginset('merch');
		if ($merch_plugin && $merch_data['is_openmerch']) {
			$statics = array('order_0' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and status=0 and (isparent=1 or (isparent=0 and parentid=0)) and paytype<>3 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'order_1' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and (status=1 or (status=0 and paytype=3)) and isparent=0 and refundid=0 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'order_2' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and (status=2 or (status=1 and sendtype>0)) and isparent=0 and refundid=0 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'order_4' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and refundstate=1 and isparent=0 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'cart' => pdo_fetchcolumn('select ifnull(sum(total),0) from ' . tablename('ewei_shop_member_cart') . ' where uniacid=:uniacid and openid=:openid and deleted=0', $params), 'favorite' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_member_favorite') . ' where uniacid=:uniacid and openid=:openid and deleted=0', $params));
		}
		else {
			$statics = array('order_0' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and status=0 and isparent=0 and paytype<>3 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'order_1' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and (status=1 or (status=0 and paytype=3)) and isparent=0 and refundid=0 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'order_2' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and (status=2 or (status=1 and sendtype>0)) and isparent=0 and refundid=0 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'order_4' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and refundstate=1 and isparent=0 and uniacid=:uniacid and istrade=0 and userdeleted=0', $params), 'cart' => pdo_fetchcolumn('select ifnull(sum(total),0) from ' . tablename('ewei_shop_member_cart') . ' where uniacid=:uniacid and openid=:openid and deleted=0 and selected = 1', $params), 'favorite' => $merch_plugin && $merch_data['is_openmerch'] ? pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_member_favorite') . ' where uniacid=:uniacid and openid=:openid and deleted=0 and `type`=0', $params) : pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_member_favorite') . ' where uniacid=:uniacid and openid=:openid and deleted=0', $params));
		}

		$newstore_plugin = p('newstore');

		if ($newstore_plugin) {
			$statics['norder_0'] = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and status=0 and isparent=0 and istrade=1 and uniacid=:uniacid', $params);
			$statics['norder_1'] = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and status=1 and isparent=0 and istrade=1 and refundid=0 and uniacid=:uniacid', $params);
			$statics['norder_3'] = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and status=3 and isparent=0 and istrade=1 and uniacid=:uniacid', $params);
			$statics['norder_4'] = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and ismr=0 and refundstate=1 and isparent=0 and istrade=1 and uniacid=:uniacid', $params);
		}

		$hascoupon = false;
		$hascouponcenter = false;
		$plugin_coupon = com('coupon');

		if ($plugin_coupon) {
			$time = time();
			$sql = 'select count(*) from ' . tablename('ewei_shop_coupon_data') . ' d';
			$sql .= ' left join ' . tablename('ewei_shop_coupon') . ' c on d.couponid = c.id';
			$sql .= ' where d.openid=:openid and d.uniacid=:uniacid and  d.used=0 ';
			$sql .= ' and (   (c.timelimit = 0 and ( c.timedays=0 or c.timedays*86400 + d.gettime >=unix_timestamp() ) )  or  (c.timelimit =1 and c.timestart<=' . $time . ' && c.timeend>=' . $time . ')) order by d.gettime desc';
			$statics['coupon'] = pdo_fetchcolumn($sql, array(':openid' => $_W['openid'], ':uniacid' => $_W['uniacid']));
			$pcset = $_W['shopset']['coupon'];

			if (empty($pcset['closemember'])) {
				$hascoupon = true;
			}

			if (empty($pcset['closecenter'])) {
				$hascouponcenter = true;
			}

			if ($hascoupon) {
				$couponnum = com('coupon')->getCanGetCouponNum($_W['merchid']);
			}
		}

		$hasglobonus = false;
		$plugin_globonus = p('globonus');

		if ($plugin_globonus) {
			$plugin_globonus_set = $plugin_globonus->getSet();
			$hasglobonus = !empty($plugin_globonus_set['open']) && !empty($plugin_globonus_set['openmembercenter']);
		}

		$haslive = false;
		$haslive = p('live');

		if ($haslive) {
			$live_set = $haslive->getSet();
			$haslive = $live_set['ismember'];
		}

		$hasThreen = false;
		$hasThreen = p('threen');

		if ($hasThreen) {
			$plugin_threen_set = $hasThreen->getSet();
			$hasThreen = !empty($plugin_threen_set['open']) && !empty($plugin_threen_set['threencenter']);
		}

		$hasauthor = false;
		$plugin_author = p('author');

		if ($plugin_author) {
			$plugin_author_set = $plugin_author->getSet();
			$hasauthor = !empty($plugin_author_set['open']) && !empty($plugin_author_set['openmembercenter']);
		}

		$hasabonus = false;
		$plugin_abonus = p('abonus');

		if ($plugin_abonus) {
			$plugin_abonus_set = $plugin_abonus->getSet();
			$hasabonus = !empty($plugin_abonus_set['open']) && !empty($plugin_abonus_set['openmembercenter']);
		}

		$card = m('common')->getSysset('membercard');
		$actionset = m('common')->getSysset('memberCardActivation');
		$haveverifygoods = m('verifygoods')->checkhaveverifygoods($_W['openid']);

		if (!empty($haveverifygoods)) {
			$verifygoods = m('verifygoods')->getCanUseVerifygoods($_W['openid']);
		}

		$showcard = 0;

		if (!empty($card)) {
			$membercardid = $member['membercardid'];
			if (!empty($membercardid) && ($card['card_id'] == $membercardid)) {
				$cardtag = '查看微信会员卡信息';
				$showcard = 1;
			}
			else {
				if (!empty($actionset['centerget'])) {
					$showcard = 1;
					$cardtag = '领取微信会员卡';
				}
			}
		}

		$hasqa = false;
		$plugin_qa = p('qa');

		if ($plugin_qa) {
			$plugin_qa_set = $plugin_qa->getSet();

			if (!empty($plugin_qa_set['showmember'])) {
				$hasqa = true;
			}
		}

		$hassign = false;
		$com_sign = p('sign');

		if ($com_sign) {
			$com_sign_set = $com_sign->getSet();
			if (!empty($com_sign_set['iscenter']) && !empty($com_sign_set['isopen'])) {
				$hassign = (empty($_W['shopset']['trade']['credittext']) ? '积分' : $_W['shopset']['trade']['credittext']);
				$hassign .= (empty($com_sign_set['textsign']) ? '签到' : $com_sign_set['textsign']);
			}
		}

		$hasLineUp = false;
		$lineUp = p('lineup');

		if ($lineUp) {
			$lineUpSet = $lineUp->getSet();
			if (!empty($lineUpSet['isopen']) && !empty($lineUpSet['mobile_show'])) {
				$hasLineUp = true;
			}
		}

		$wapset = m('common')->getSysset('wap');
		$appset = m('common')->getSysset('app');
		$needbind = false;
		if (empty($member['mobileverify']) || empty($member['mobile'])) {
			if ((empty($_W['shopset']['app']['isclose']) && !empty($_W['shopset']['app']['openbind'])) || !empty($_W['shopset']['wap']['open']) || $hasThreen) {
				$needbind = true;
			}
		}

		if (p('mmanage')) {
			$roleuser = pdo_fetch('SELECT id, uid, username, status FROM' . tablename('ewei_shop_perm_user') . 'WHERE openid=:openid AND uniacid=:uniacid AND status=1 LIMIT 1', array(':openid' => $_W['openid'], ':uniacid' => $_W['uniacid']));
		}
		

		$member = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member') . ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], ':openid' => $_W['openid']));
		if(substr($member['avatar'], -6) == '132132'){
			$member['avatar'] = substr($member['avatar'], 0, -3);
			// pdo_upload('ewei_shop_member', array('avatar' => $member['avatar']), array('uniacid' => $_W['uniacid'], 'openid' => $_W['openid']));
		}

		// 会员身份信息
		$identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity_member') . ' WHERE uniacid = :uniacid AND shop_user_id = :shop_user_id', array(':uniacid' => $_W['uniacid'], ':shop_user_id' => $member['uid']));
		$set = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity_setting') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));

		include $this->template();
	}

	// 我的权限
	public function jurisdiction(){
		global $_W;
		global $_GPC;

		$member = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member') . ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], ':openid' => $_W['openid']));
		$identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity_member') . ' WHERE uniacid = :uniacid AND shop_user_id = :shop_user_id', array(':uniacid' => $_W['uniacid'], ':shop_user_id' => $member['uid']));
		$identity_level = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND id = :id', array(':id' => $identity['identity_id'], ':uniacid' => $_W['uniacid']));

		if(empty($identity['identity_id'])){
			$identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND levelnum = 0', array(':uniacid' => $_W['uniacid']));
		}
		
		$identity['start_time'] = date('Y-m-d H:i:s', $identity['start_time']);
		$identity['end_time'] = date('Y-m-d H:i:s', $identity['end_time']);
	
		//会员等级
		$member_level = pdo_fetchall('SELECT id,levelname FROM ' . tablename('ewei_shop_member_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($member_level as $key => $value) {
			if($identity['is_use_member_level'] == '1'){
				if($identity_level['shop_user_grade_id'] == $value['id']){
					$identity['member_level'] = $value['levelname'];
				}
				if($identity_level['end_shop_user_grade_id'] == $value['id']){
					$identity['end_member_level'] = $value['levelname'];
				}
			}

			if(empty($identity) && $member['level'] == $value['id']){
				$identity['member_level'] = $value['levelname'];
			}
		}

		//分销等级
		$distribution = pdo_fetchall('SELECT id,levelname FROM ' . tablename('ewei_shop_commission_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($distribution as $key => $value) {
			if($identity['is_use_distribution_level'] == '1'){
				if($identity_level['distribution_grade_id'] == $value['id']){
					$identity['distribution_level'] = $value['levelname'];
				}
				if($identity_level['end_distribution_grade_id'] == $value['id']){
					$identity['end_distribution_level'] = $value['levelname'];
				}
			}

			if(empty($identity) && $member['isagent'] == 1 && $member['agentlevel'] == $value['id']){
				$identity['distribution_level'] = $value['levelname'];
			}
		}

		// 门店等级
		$mendian = pdo_fetchall('SELECT levelname,id FROM ' . tablename('ewei_shop_mendian_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		$mendian_info = pdo_fetch('SELECT mendian_level FROM ' . tablename('ewei_shop_mendian_info') . ' WHERE uniacid = :uniacid AND uid = :uid AND status = 1 AND end_time > :end_time', array(':uid' => $member['uid'], ':uniacid' => $_W['uniacid'], ':end_time' => time()));
		foreach ($mendian as $key => $value) {
			if($identity['is_use_mendian'] == '1'){
				if($identity_level['mendian_level_id'] == $value['id']){
					$identity['mendian_level'] = $value['levelname'];
				}
				if($identity_level['end_mendian_level_id'] == $value['id']){
					$identity['end_mendian_level'] = $value['levelname'];
				}
			}

			if(empty($identity) && $mendian_info['mendian_level'] == $value['id']){
				$identity['mendian_level'] = $value['levelname'];
			}
		}

		// 多商户分组
		$merch = pdo_fetchall('SELECT groupname,id FROM ' . tablename('ewei_shop_merch_group') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		$merch_info = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_merch_user') . ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], ':openid' => $member['openid']));
		foreach ($merch as $key => $value) {
			if($identity['is_use_merch'] == '1'){
				if($identity_level['merch_id'] == $value['id']){
					$identity['merch_level'] = $value['groupname'];
				}
				if($identity_level['end_merch_id'] == $value['id']){
					$identity['end_merch_level'] = $value['groupname'];
				}
			}

			if(empty($identity) && $merch_info['mendian_level'] == $value['id']){
				$identity['merch_level'] = $value['levelname'];
			}
		}

		// 全民股东等级
		$globonus = pdo_fetchall('SELECT levelname,id FROM ' . tablename('ewei_shop_globonus_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($globonus as $key => $value) {
			if($identity['is_use_globonus_partner'] == '1'){
				if($identity_level['globonus_partner_id'] == $value['id']){
					$identity['globonus_level'] = $value['levelname'];
				}
				if($identity_level['end_globonus_partner_id'] == $value['id']){
					$identity['end_globonus_level'] = $value['levelname'];
				}
			}

			if(empty($identity) && $member['ispartner'] == 1 && $member['partnerlevel'] == $value['id']){
				$identity['globonus_level'] = $value['levelname'];
			}
		}

		// 区域代理等级
		$abonus = pdo_fetchall('SELECT levelname,id FROM ' . tablename('ewei_shop_abonus_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($abonus as $key => $value) {
			if($identity['is_use_abonus_grade'] == '1'){
				if($identity_level['abonus_agent_id'] == $value['id']){
					$identity['abonus_level'] = $value['levelname'];
				}
				if($identity_level['end_abonus_agent_id'] == $value['id']){
					$identity['end_abonus_level'] = $value['levelname'];
				}				
			}

			if(empty($identity) && $member['isaagent'] == 1 && $member['aagentlevel'] == $value['id']){
				$identity['abonus_level'] = $value['levelname'];
			}
		}
		
		include $this->template('identity/jurisdiction');
	}

	// 我的身份
	Public function myIdentity(){
		global $_W;
		global $_GPC;

		$member = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member') . ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], ':openid' => $_W['openid']));
		$identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity_member') . ' WHERE uniacid = :uniacid AND shop_user_id = :shop_user_id', array(':uniacid' => $_W['uniacid'], ':shop_user_id' => $member['uid']));
		$identity_level = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND id = :id', array(':id' => $identity['identity_id'], ':uniacid' => $_W['uniacid']));

		$identity['start_time'] = date('Y-m-d H:i:s', $identity['start_time']);
		$identity['end_time'] = date('Y-m-d H:i:s', $identity['end_time']);
	

		include $this->template('identity/myIdentity');
	}

	protected function get_mendian($id) {
		global $_W;
		$member = pdo_fetch('SELECT id,parent_id,is_use_mendian,shop_user_id FROM ' . tablename('ewei_shop_identity_member') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $id));

		
		if($member['is_use_mendian']  == '1'){
			$res = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_info') . ' WHERE uniacid = :uniacid AND uid = :uid AND status = 1',array(':uniacid' => $_W['uniacid'], ':uid' => $member['shop_user_id']));

		} else {
			if($member['parentid'] > 0){
				$res = $this->get_mendian($member['parent_id']);
			} else {
				$res = pdo_fetch('SELECT * FROM' . tablename('ewei_shop_mendian_info') . ' WHERE uniacid = :uniacid AND is_main = 1 AND status = 1', array(':uniacid' => $_W['uniacid']));
				
			}
		}
		
		return $res;
	}
}

?>
